//
//  CAttentionMgr.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/3/2.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "CAttentionMgr.h"
#import "FMDB.h"
#import "CMineAttentionContact.h"

@implementation CAttentionMgr

//插入最近联系人
-(BOOL)insertAttentionContact:(CMineAttentionContact *)attentionContact
{
    if(attentionContact == nil || M_ISSTRNULL(attentionContact.userId))
    {
        return NO;
    }
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        FMResultSet *result = [db executeQuery:@"select * from ATTENTIONCONTACT where userId = ? AND isAttentionMe = ?",attentionContact.userId,attentionContact.isAttentionMe?@1:@0];
        
        if ([result next]) {
            ret = YES;
        }
        if (ret) {
            
        } else {
            ret = [db executeUpdate:@"INSERT INTO ATTENTIONCONTACT(UserId,isAttentionMe,yuLiuZiduan1, yuLiuZiduan2, yuLiuZiduan3) VALUES (?,?,?,?,?)", M_STOREVALUE(attentionContact.userId),attentionContact.isAttentionMe?@1:@0,M_STOREVALUE(attentionContact.yuLiuZiduan1),@(attentionContact.yuLiuZiduan2),M_STOREVALUE(attentionContact.yuLiuZiduan3)];
        }
       
        
    }];
    return ret;
}
//批量插入最近联系人
-(BOOL)batchInsertAttentionContact:(NSArray *)attentionContactList
{
    if(attentionContactList == nil || [attentionContactList count] <= 0)
    {
        return NO;
    }
    
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(CMineAttentionContact *attentionContact in attentionContactList)
        {
            if(attentionContact != nil && !M_ISSTRNULL(attentionContact.userId))
            {
                ret = [db executeUpdate:@"INSERT INTO ATTENTIONCONTACT(UserId,isAttentionMe,yuLiuZiduan1, yuLiuZiduan2, yuLiuZiduan3) VALUES (?,?,?,?,?)", M_STOREVALUE(attentionContact.userId),attentionContact.isAttentionMe?@1:@0,M_STOREVALUE(attentionContact.yuLiuZiduan1),@(attentionContact.yuLiuZiduan2),M_STOREVALUE(attentionContact.yuLiuZiduan3)];
                if(!ret)
                {
                    *rollback = YES;
                    break;
                }
            }
        }
        
    }];
    return ret;
}
//删除最近联系人
-(BOOL)delAttentionContact:(NSString *)UserId
{
    if(M_ISSTRNULL(UserId))
    {
        return NO;
    }
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"delete from ATTENTIONCONTACT where UserId = ? AND isAttentionMe = 0", UserId];
    }];
    return ret;
}
//加载所有最近联系人
-(NSArray *)loadAllAttentionContacts
{
    NSString *sql = nil;
    __block NSMutableArray *array = [NSMutableArray arrayWithCapacity:5];
    sql = [NSString stringWithFormat:@"select * from ATTENTIONCONTACT" ];
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMineAttentionContact *attentionContact = [[CMineAttentionContact alloc] init];
            attentionContact.userId = [result stringForColumn:@"UserId"];
            attentionContact.AttentionContactIndex = [result longLongIntForColumn:@"identifer"];
            attentionContact.isAttentionMe = [result boolForColumn:@"isAttentionMe"];
            [array addObject:attentionContact];
            attentionContact = nil;
        }
        [result close];
    }];
    return array;
}

- (CMineAttentionContact *)loadAttentionContactWithUserId:(NSString *)userId isAttentionMe:(BOOL)isAttentionMe
{
    if (M_ISSTRNULL(userId)) {
        return nil;
    }
    __block CMineAttentionContact *attentionContact = nil;
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from ATTENTIONCONTACT where userId = ? AND isAttentionMe = ?",userId,isAttentionMe?@1:@0];
        while ([result next]) {
            attentionContact = [[CMineAttentionContact alloc] init];
            attentionContact.userId = [result stringForColumn:@"UserId"];
            attentionContact.isAttentionMe = [result boolForColumn:@"isAttentionMe"];
            attentionContact.AttentionContactIndex = [result longLongIntForColumn:@"identifer"];
        }
        [result close];
    }];
    return attentionContact;
    
}

- (BOOL)delAllAttentionContacts
{
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"delete from ATTENTIONCONTACT"];
    }];
    return ret;
    
}

- (NSArray *)loadAttentionContactsWithIsAttentionMe:(BOOL)isAttentionMe
{
    NSString *sql = nil;
    __block NSMutableArray *array = [NSMutableArray arrayWithCapacity:5];
    sql = [NSString stringWithFormat:@"select * from ATTENTIONCONTACT where isAttentionMe = \'%@\'",isAttentionMe?@1:@0];
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMineAttentionContact *attentionContact = [[CMineAttentionContact alloc] init];
            attentionContact.userId = [result stringForColumn:@"UserId"];
            attentionContact.AttentionContactIndex = [result longLongIntForColumn:@"identifer"];
            attentionContact.isAttentionMe = [result boolForColumn:@"isAttentionMe"];
            [array addObject:attentionContact];
            attentionContact = nil;
        }
        [result close];
    }];
    return array;

}
@end
